<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\Admin;

use App\Controller\AppBaseController;
use App\Model\Act;
use App\Model\ActVoteMedia;
use App\Model\ActVoteJoin;
use App\Model\Party;
use App\Model\User;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\AdminApiAuth;
use Hyperf\DbConnection\Db;

use PhpOffice\PhpSpreadsheet\IOFactory;

#[Controller(prefix: 'admin')]
#[Middleware(AdminApiAuth::class)]
class ActVoteController extends AppBaseController
{
    /**
     * 后台--活动投票--作品|列表
     * @param int $act_id
     * @return object
     */
    #[RequestMapping(path:'actVoteMediaList/{act_id}',methods: 'get')]
    public function actVoteMediaList(int $act_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            //查询条件
            $where = [];
            $where['act_id'] = $act_id;

            $list = ActVoteMedia::where($where)->offset($offset)->limit($limit)->get();

            $total = 0;
            $next = 0;
            if($list->isNotEmpty()){
                if($page==1){
                    $total = ActVoteMedia::where($where)->count();
                }
                if(count($list) >= $limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',[
                'list' => $list, 'next' => $next, 'total'=>$total
            ]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 后台--活动投票--作品|详情
     * @param int $media_id
     * @return object
     */
    #[RequestMapping(path:'actVoteMediaInfo/{media_id}',methods: 'get')]
    public function actVoteMediaInfo(int $media_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $info = ActVoteMedia::where('id',$media_id)->first();
            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }



    /**
     * 后台--活动投票--作品|编辑
     * @param int|null $media_id
     * @return object
     */
    #[RequestMapping(path: 'actVoteMediaEdit[/{media_id}]',methods: 'post,get')]
    public function actVoteMediaEdit(int|null $media_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            //获取所有参数
            $data = $this->request->all();

            //虚拟数据
//            if(empty($data)){
//                $data['act_id'] = 33;
//                $data['title'] = '第一次投票互动';
//                $data['photo'] = ['uploads/alioss/consult1719480453860.png'];
//                $data['video'] = ['uploads/alioss/consult1719480453860.png'];
//            }
            //虚拟数据

            if( empty($data['act_id']) || !is_numeric($data['act_id']) ||
                empty($data['title']) ||
                empty($data['photo'])
            ){
                return $this->reMsg->re(10007,'参数丢失');
            }

            if(!isset($data['video']))  $data['video'] = null;
            isset($data['status']) && $data['status']==0 ? $data['status'] = 0 : $data['status'] = 1;

            $actInfo = Act::where('id',$data['act_id'])->first();
            if(empty($actInfo)) return $this->reMsg->re(10008);

            //开启事务
            Db::beginTransaction();
            //整理数据
            $insertData['party_id'] = $actInfo->party_id;
            $insertData['act_id'] = $actInfo->id;
            $insertData['title'] = $data['title'];
            $insertData['photo'] = $data['photo'];
            $insertData['video'] = $data['video'];
            $insertData['status'] = $data['status'];

            if($media_id){
                //编辑
                $info = ActVoteMedia::where('id',$media_id)->first();
                if(empty($info)) return $this->reMsg->re(10008);
                $info->act_id = $insertData['act_id'];
                $info->title = $insertData['title'];
                $info->photo = $insertData['photo'];
                $info->video = $insertData['video'];
                $info->status = $insertData['status'];

                $info->party_id = $actInfo->party_id;
                $info->admin_id = $_admin->id;
                $info->save();
            }else{
                //新建
                $insertData['party_id'] = $actInfo->party_id;
                $insertData['admin_id'] = $_admin->id;
                ActVoteMedia::create($insertData);
            }
            Db::commit();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            Db::rollBack();
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 后台--活动投票--作品|删除
     * @param int $media_id
     * @return object
     */
    #[RequestMapping(path:'actVoteMediaDelete/{media_id}',methods: 'get')]
    public function actVoteMediaDelete(int $media_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            //判断信息
            $info = ActVoteMedia::where('id',$media_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);

            //删除问题
            $info->delete();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }





    #[RequestMapping(path: 'pushList',methods: 'get')]
    public function pushList(): object
    {
        $data['push_all'] = 0;
//        $data['push_user'] = [1,2,3,4];
//        $data['push_tip'] = [1,2,8,10];
        $data['push_party'] = [1,2];


        //推送全员
        $userList = [];
        if(!empty($data['push_all']) && $data['push_all']==1){
            $userList = User::where('status',1)->whereNotNull('auth_time')->pluck('id')->toArray();
        }else{
            //指定推送
            if(!empty($data['push_user']) && $data['push_user']){
                $userList = User::where('status',1)->whereNotNull('auth_time')->whereIn('id',$data['push_user'])->pluck('id')->toArray();
            }else if(!empty($data['push_tip']) && $data['push_tip']){
                $userList = User::where('status',1)->whereNotNull('auth_time')->whereIn('user_type',$data['push_tip'])->pluck('id')->toArray();
            }else if(!empty($data['push_party']) && $data['push_party']){
                $userList = User::where('status',1)->whereNotNull('auth_time')->whereIn('party_id',$data['push_party'])->pluck('id')->toArray();
            }
            if(empty($userList)) return $this->reMsg->re(0,'推送标签未选择');
        }
        return $this->reMsg->re(0,'test',$userList);
    }








}